﻿using Abp.Utils;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Abp.NPOI
{
    public class NPOICellElement
    {
        /// <summary>
        /// 
        /// </summary>
        public NPOIRowElement Row { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ICell Cell { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int ColumnIndex { get; set; }

        public const int WidthConstant = 256;

       
        public NPOICellElement(NPOIRowElement row, int columnIndex, VerticalAlignment VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment Alignment = HorizontalAlignment.Center)
        {
            this.Row = row;
            this.ColumnIndex = columnIndex;
            this.Cell = this.Row.Row.CreateCell(this.ColumnIndex, CellType.Blank);
            this.Cell.SetCellValue(string.Empty);
            this.Cell.CellStyle = NPOIRowElement.Style;
        }

       
        public NPOICellElement SetValue(string value)
        {
            this.Cell.SetCellValue(value);
            this.Cell.SetCellType(CellType.String);
            return this;
        }

        public NPOICellElement SetValue(bool value)
        {
            this.Cell.SetCellValue(value);
            this.Cell.SetCellType(CellType.Boolean);
            return this;
        }

        public NPOICellElement SetValue(double value)
        {
            this.Cell.SetCellValue(value);
            this.Cell.SetCellType(CellType.Numeric);
            return this;
        }

        public NPOICellElement SetValue(DateTime value)
        {
            this.Cell.SetCellValue(value);
            return this;
        }

        public NPOICellElement SetRowSpan(int rowspan)
        {
            this.Row.Sheet.AddMergedRegion(new CellRangeAddress(this.Row.RowIndex, this.Row.RowIndex + rowspan - 1, this.ColumnIndex, this.ColumnIndex));
            return this;
        }

        public NPOICellElement SetColSpan(int colspan)
        {
            this.Row.Sheet.AddMergedRegion(new CellRangeAddress(this.Row.RowIndex, this.Row.RowIndex, this.ColumnIndex, this.ColumnIndex + colspan - 1));
            return this;
        }

        public NPOICellElement SetWidth(int width)
        {
            this.Row.Sheet.SetColumnWidth(this.ColumnIndex, width * WidthConstant);
            return this;
        }


    }
}
